<?php
namespace app\api\controller;

class Login extends Api
{
    public function index()
    {
        $map['username'] = $this->input_data['username'];
        $map['status'] = 1;
        $user = db('Admin')->where($map)->find();
        if($user && $user['password'] == passwordAdmin($this->input_data['password']))
        {
            //生成token
            $token = $user['token'] != '' ? $user['token'] : md5( $user['id'].time().rand('0',999) );
            $user['token'] = $token;
            $logs['cate'] = 1;
            $logs['uid'] = $user['id'];
            $logs['login_time'] = $user['last_login_time'] = $user['updated_at'] = time();
            $user['last_act_time'] = time()-10;
            $logs['login_ip'] = $user['last_ip'] = request()->ip();
            //写入登录日志
            db('LogsLogin')->insert($logs);
            $user['login_count'] = $user['login_count'] + 1;
            #更新用户表
            db('Admin')->update($user);

            $data = array(
                'code' => $this->suc_code,
                'data' => $token,
            );   
        }
        else
        {
            $data = array(
                'code' => $this->err_code,
                'data' => '账号密码不正确',
            );
        }

        $this->res($data);
    }

    public function info()
    {
        $user =  array(
            'roles' => 'admin',
            'introduction' => 'I am a super administrator',
            'avatar' => 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
            'name' => 'Super Admin'
        );

        $data = array(
            'code' => 20000,
            'data' => $user,
        );
        $this->res($data);
    }

    
    public function logout()
    {
        // 退出登录的代码

        $data = array(
            'code' => 20000,
            'data' => 'success',
        );
        return base64_encode(json_encode($data,JSON_UNESCAPED_UNICODE));
    }
}
